<pre class="metadata">
title: Types as Comments Tentative Grammar
stage: 0
status: proposal
location: https://github.com/tc39/proposal-type-annotations/
contributors: Anders Hejlsberg, Daniel Rosenwasser
copyright: false
</pre>

<aside>
<p>
This grammar is still in a tentative state, and changes are expected over time.
It is not a comprehensive specification and it does not contain any formal semantic rules, but the intent is that all syntactic additions can be interpreted as a form of comments.
Just as implementations typically do not provide runtime semantics for comments, the type syntax outlined here should not introduce noticeable runtime semantics.
</p>
<p>
Any issues with the grammar in this document should be <a href="https://github.com/tc39/proposal-type-annotations/">reported on the proposal repository</a>.
</p>
</aside>

<emu-clause id="grammar-conventions">
<h1>Organization and Notational Conventions</h1>

<p>Currently this grammar is split into <emu-xref href="#modified-productions">modified productions of ECMA-262</emu-xref> and <emu-xref href="#new-productions">new productions</emu-xref> that are specific to this proposal.</p>

<p>This tentative grammar takes some notational liberties beyond the conventions in ECMA-262.
This is a temporary accommodation for authors in order to avoid extensive duplication of ECMA-262's grammar.
Within <emu-xref href=#modified-productions title></emu-xref>, RHS productions with <ins>insert-formatted text (such as this text)</ins> indicates the addition of these RHS productions to existing productions in ECMA-262.
Otherwise, RHS productions in <emu-xref href=#modified-productions title></emu-xref> are intended to replace the original productions in ECMA-262.</p>
</emu-clause>
<emu-clause id="modified-productions">
<h1>Modified Productions</h1>
<br />
<br />
<emu-grammar>
MemberExpression :
    <ins>
    MemberExpression  `::`  TypeArguments
    MemberExpression  [no LineTerminator here]  `!`
    </ins>

CallExpression :
    <ins>
    CallExpression  `::`  TypeArguments
    </ins>

OptionalChain :
    <ins>
    OptionalChain  `::`  TypeArguments
    </ins>

RelationalExpression :
    <ins>
    RelationalExpression  [no LineTerminator here]  `as`  Type
    RelationalExpression  [no LineTerminator here]  `as`  `const`
    </ins>

Declaration :
    <ins>
    TypeDeclaration
    InterfaceDeclaration
    </ins>

LexicalBinding :
    BindingIdentifier  TypeAnnotation?  Initializer?
    BindingPattern  TypeAnnotation?  Initializer

VariableDeclaration :
    BindingIdentifier  TypeAnnotation?  Initializer?
    BindingPattern  TypeAnnotation?  Initializer

Catch :
    `catch`  `(`  CatchParameter  TypeAnnotation?  `)`  Block
    `catch`  Block

FormalParameter :
    BindingIdentifier  TypeAnnotation?  Initializer?
    BindingIdentifier  `?`  TypeAnnotation?
    BindingPattern  TypeAnnotation?  Initializer?

FunctionRestParameter :
    `...`  BindingIdentifier  TypeAnnotation?
    `...`  BindingPattern  TypeAnnotation?

FunctionDeclaration :
    `function`  BindingIdentifier  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  FunctionBody  `}`

FunctionExpression :
    `function`  BindingIdentifier?  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  FunctionBody  `}`

ArrowFunction :
    <ins>
    ArrowParameters  TypeAnnotation  `=>`  ConciseBody
    </ins>

ArrowParameters :
    <ins>
    TypeParameters  `(`  UniqueFormalParameters  `)`
    </ins>

MethodDefinition :
    ClassElementName  `?`?  TypeParameters?  `(`  UniqueFormalParameters  `)`  TypeAnnotation?  `{`  FunctionBody  `}`
    GeneratorMethod
    AsyncMethod
    AsyncGeneratorMethod
    `get`  ClassElementName  `(`  `)`  TypeAnnotation?  `{`  FunctionBody  `}`
    `set`  ClassElementName  `(`  PropertySetParameterList  `)`  `{`  FunctionBody  `}`

GeneratorDeclaration :
    `function`  `*`  BindingIdentifier  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  GeneratorBody  `}`

GeneratorExpression :
    `function`  `*`  BindingIdentifier?  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  GeneratorBody  `}`

GeneratorMethod :
    `*`  ClassElementName  `?`?  TypeParameters?  `(`  UniqueFormalParameters  `)`  TypeAnnotation?  `{`  GeneratorBody  `}`

AsyncGeneratorDeclaration :
    `async`  [no LineTerminator here]  `function`  `*`  BindingIdentifier  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  AsyncGeneratorBody  `}`

AsyncGeneratorExpression :
    `async`  [no LineTerminator here]  `function`  `*`  BindingIdentifier?  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  AsyncGeneratorBody  `}`

AsyncGeneratorMethod :
    `async`  [no LineTerminator here]  `*`  ClassElementName  `?`?  TypeParameters?  `(`  UniqueFormalParameters  `)`  TypeAnnotation?  `{`  AsyncGeneratorBody  `}`

ClassDeclaration :
    AbstractModifier?  `class`  BindingIdentifier  ClassTail

ClassExpression :
    AbstractModifier?  `class`  BindingIdentifier?  ClassTail

ClassHeritage :
    `extends`  LeftHandSideExpression  TypeArguments?  ClassImplementsClause?

FieldDefinition :
    ClassElementName  `?`?  TypeAnnotation?  Initializer?
    ClassElementName  `!`  TypeAnnotation?  Initializer?

ClassElement :
    AccessibilityModifier?  OverrideModifier?  MethodDefinition
    AccessibilityModifier?  `static`  OverrideModifier?  MethodDefinition
    AccessibilityModifier?  FieldDefinition
    AccessibilityModifier?  `static`  FieldDefinition
    ClassStaticBlock
    AbstractClassElement
    IndexSignature
    `;`

AsyncFunctionDeclaration :
    `async`  [no LineTerminator here]  `function`  BindingIdentifier  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  AsyncFunctionBody  `}`

AsyncFunctionExpression :
    `async`  [no LineTerminator here]  `function`  BindingIdentifier?  TypeParameters?  `(`  FormalParameters  `)`  TypeAnnotation?  `{`  AsyncFunctionBody  `}`

AsyncMethod :
    `async`  [no LineTerminator here]  ClassElementName  `?`?  TypeParameters?  `(`  UniqueFormalParameters  `)`  TypeAnnotation?  `{`  AsyncFunctionBody  `}`

CoverCallExpressionAndAsyncArrowHead :
    MemberExpression  TypeParameters?  Arguments  TypeAnnotation?

AsyncArrowHead :
    `async`  [no LineTerminator here]  TypeParameters?  ArrowFormalParameters

ImportDeclaration :
    <ins>
    `import`  `type`  ImportClause  FromClause  `;`
    </ins>

ImportSpecifier :
    <ins>
    `type`  ImportedBinding
    `type`  ModuleExportName  `as`  ImportedBinding
    </ins>

</emu-grammar>
</emu-clause>

<emu-clause id="new-productions">
<h1>New Productions</h1>
<br />
<emu-grammar>

TypeArguments :
    AngleBracketedTokens

TypeDeclaration :
    `type`  BindingIdentifier  TypeParameters?  `=`  Type

TypeParameters :
    AngleBracketedTokens

Type :
    ConditionalType
    NonConditionalType

ConditionalType :
    NonConditionalType  [no LineTerminator here]  `extends`  NonConditionalType  `?`  Type  `:`  Type

NonConditionalType :
    UnionType
    FunctionType
    ConstructorType

UnionType :
    `|`?  IntersectionType
    UnionType  `|`  IntersectionType

IntersectionType :
    `&`?  TypeOperatorType
    IntersectionType  `&`  TypeOperatorType

TypeOperatorType :
    `readonly`  TypeOperatorType
    `keyof`  TypeOperatorType
    `unique`  TypeOperatorType
    `infer`  TypeOperatorType
    `not`  TypeOperatorType
    PrimaryType

PrimaryType :
    ParenthesizedType
    SquareBracketedType
    CurlyBracketedType
    TypeReference
    ArrayType
    LiteralType
    TypeQuery
    ImportType
    TypePredicate
    `this`
    `void`

ParenthesizedType :
    ParenthesizedTokens

SquareBracketedType :
    SquareBracketedTokens

CurlyBracketedType :
    CurlyBracketedTokens

TypeReference :
    TypeName  [no LineTerminator here]  TypeArguments?

TypeName :
    Identifier
    TypeName  `.`  Identifier

ArrayType :
    PrimaryType  [no LineTerminator here]  `[`  `]`

LiteralType :
    NumericLiteralType
    StringLiteral
    TemplateLiteralType
    `true`
    `false`
    `null`

TemplateLiteralType :
    NoSubstitutionTemplate
    TemplateBracketedTokens

NumericLiteralType :
    NumericLiteral
    `-`  [no LineTerminator here]  NumericLiteral

TypeQuery :
    `typeof`  [no LineTerminator here]  EntityName

EntityName :
    IdentifierName
    ImportSpecifier
    EntityName  `.`  IdentifierName
    EntityName  `::`  TypeArguments

ImportSpecifier :
    `import`  [no LineTerminator here]  `(`  ModuleSpecifier  `)`

ImportType :
    ImportSpecifier
    ImportSpecifier  `.`  TypeName

TypePredicate :
    IdentifierOrThis  [no LineTerminator here]  `is`  Type
    `asserts`  IdentifierOrThis
    `asserts`  IdentifierOrThis  [no LineTerminator here]  `is`  Type

IdentifierOrThis :
    Identifier
    `this`

FunctionType :
    TypeParameters?  ParameterList  `=>`  Type

ConstructorType :
    `new`  TypeParameters?  ParameterList  `=>`  Type

ParameterList :
    ParenthesizedTokens

InterfaceDeclaration :
    `interface`  BindingIdentifier  TypeParameters?  InterfaceExtendsClause?  InterfaceBody

InterfaceExtendsClause :
    `extends`  ClassOrInterfaceTypeList

ClassOrInterfaceTypeList :
    TypeReference
    ClassOrInterfaceTypeList  `,`  TypeReference

InterfaceBody :
    CurlyBracketedTokens

TypeAnnotation :
    `:`  Type

AbstractModifier :
    `abstract`

ClassImplementsClause :
    `implements`  ClassOrInterfaceTypeList

AccessibilityModifier :
    `public`
    `protected`
    `private`

OverrideModifier :
    `override`

AbstractClassElement :
    AccessibilityModifier?  `abstract`  OverrideModifier?  AbstractMethodDefinition
    AccessibilityModifier?  `abstract`  AbstractFieldDefinition

AbstractMethodDefinition :
    ClassElementName  TypeParameters?  `(`  UniqueFormalParameters  `)`  TypeAnnotation?
    `get`  ClassElementName  `(`  `)`  TypeAnnotation?
    `set`  ClassElementName  `(`  PropertySetParameterList  `)`

AbstractFieldDefinition :
    ClassElementName  `?`?  TypeAnnotation?

IndexSignature :
    `[`  BindingIdentifier  TypeAnnotation  `]`  TypeAnnotation

// New token section productions

BracketedTokens :
    ParenthesizedTokens
    SquareBracketedTokens
    CurlyBracketedTokens
    AngleBracketedTokens
    TemplateBracketedTokens

ParenthesizedTokens :
    `(`  TokenBody?  `)`

SquareBracketedTokens :
    `[`  TokenBody?  `]`

CurlyBracketedTokens :
    `{`  TokenBody?  `}`

AngleBracketedTokens :
    `<`  TokenBody?  `>`

TemplateBracketedTokens :
    TemplateHead  TemplateTokenBody  TemplateTail

TemplateTokenBody :
    TokenBody
    TokenBody  TemplateMiddle  TemplateTokenBody

TokenBody :
    TokenOrBracketedTokens  TokenBody?

TokenOrBracketedTokens :
    NonBracketedToken
    BracketedTokens

NonBracketedToken :
    Token but not one of `(` or `)` or `[` or `]` or `{` or `}` or `<` or `>` or TemplateHead or TemplateMiddle or TemplateTail

</emu-grammar>
</emu-clause>
